<?php 
namespace app\superadmin\controller;
use think\Db;
use think\Controller;
class User extends Base{
	public function index(){
   
		$users=Db::name('users')->where("uid>'0'")->paginate(10);

    //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);
    // dump($users);
		$this->assign('users',$users);
		return $this->fetch();
	}
  //屏蔽/解除屏蔽
	public function shield($id){
    // dump($id);
    
    $res=Db::name('users')->where('uid',$id)->update(['ustatus'=>1]);
    if ($res) {
      # code...
      $data=Db::name('users')->where('uid',$id)->select();

    // dump($data[0]['email']);
    sendMail($data[0]["email"],"帮帮通知","你的账号存在多次违规行为，已被管理员封禁，详情请联系管理员。管理员邮箱：1780187448@qq.com");
    $this->redirect('index');
    }else{
      $this->error('封禁失败！发送邮件失败！');
    }
	}
  public function shield2($id){
    $res=Db::name('users')->where('uid',$id)->update(['ustatus'=>0]);
    if ($res) {
      # code...
      $data=Db::name('users')->where('uid',$id)->select();

    // dump($data[0]['email']);
    sendMail($data[0]["email"],"帮帮通知","您的账号已经解封，请规范您在本网站的操作行为。管理员邮箱：1780187448@qq.com");

    $this->redirect('index');
    }else{
      $this->error('解除封禁失败！发送邮件失败！');
    }
  }

  //重置密码
	public function resetpassword($id){
  if(Db::name("users")->where("uid='$id'")->update(['password'=>md5('0000000')])){
    $this->redirect('index');
  }
  else{
    $this->error('重置密码失败');
  }

	}
  //添加新用户
  public function addusers(){
    $data=input("post.");
    // dump($data);
    $file = request()->file('upic');
    // dump($file);
    $picname="";
    if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'upload');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }

    }
    unset($data["password"]);
    $data+=[
            'upic'=>$picname,
            'ustatus'=>0,
            'password'=>md5(input('password')),
            ];
    $num=Db::name('users')->where('uname',$data['uname'])->count();
    if ($num>0) {
      # code...
      $this->error('用户名重名，请重新添加！');
    }
    $res=Db::name('users')->insert($data);
    if ($res) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('添加失败!');
    }

  }
  //搜索
  public function find(){
  $findname=input('findname');
  $findemail=input('findemail');
  $findid=input('findid');

  if ($findname!=NULL) {
    $registration=Db::query("select * from `users` where uname='$findname'");
    $this->assign("users",$registration);
    return $this->fetch("index");
  }elseif ($findemail!=NULL) {
    $registration=Db::query("select * from `users` where eamil='$findemail'");
    $this->assign("users",$registration);
    return $this->fetch("index");
  }elseif ($findid!=NULL) {
    $registration=Db::query("select * from `users` where uid='$findid'");
    $this->assign("users",$registration);
    return $this->fetch("index");
  }else{
    $registration=Db::query("select * from `users`");
    $this->assign("users",$registration);
    return $this->fetch("index");
  }
}
  //修改用户信息页
  public function update($uid){
    $userupdate=Db::name('users')->where('uid',$uid)->select();
    $this->assign('userupdate',$userupdate);
    return $this->fetch();
  }
   //更新用户信息
  public function doupdate($uid){
    // dump($sid);
    $data=input("post.");
    // dump($data);

    // if ($spic!=0) {
    //   $row=Db::name('star_xy')->where('sid',$sid)->select();
    //   unlink("../public/static/upload".$row["spic"]);
    // }
    $file = request()->file('upic');
     // dump($files);
    // dump($file);
    $picname="";
    if ($file==null) {
      # code...
       $row=Db::name('users')->where('uid',$uid)->select();
      $file=$row[0]['upic'];
      $picname=$row[0]['upic'];
       // dump($row[0]['spic']);
    }else{
     
       // if($file){
       
          # code...
          $info=$file->rule('uniqid')->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH.'public'.DS.'static'.DS.'upload');
          if ($info) {
          # code...
          $picname=$info->getFilename();
        }else{
          // echo $f->getError();
          $this->error($file->getError());
        }
        
        $row1=Db::name('users')->where('uid',$uid)->select();
        unlink('../public/static/upload/'.$row1[0]['upic']);

     
      
    }
     $data+=['upic'=>$picname];
    $up=Db::name('users')->where('uid',$uid)->update($data);
    if ($up) {
      # code...
      $this->redirect('index');
    }else{
      $this->error('请至少修改一条信息');
    }
  }

  //详情页面
  public function userdetails($uid){
    $userdetails=Db::name("users")->where('uid',$uid)->select();
    $collects=Db::name("collect c")->join('videos v','c.vid=v.vid')->join('videotype t','v.tid=t.tid')->where('uid',$uid)->paginate(5);
    $tables=Db::name('videotable')->alias('t')->join('videos v','t.vid=v.vid')->join('videotype p','v.tid=p.tid')->where('uid',$uid)->paginate(5);

    //序号
    $page=request()->param('page');
    $page||($page=1);
    $this->assign('page',$page);


    $this->assign('tables',$tables);
    $this->assign('userdetails',$userdetails);
    $this->assign('collects',$collects);
    return $this->fetch();
  }
}